package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrOptional;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;

public class FjSApdu extends AxdrType {
    private byte[] dataCode = null;
    private AxdrOptional<FollowReport> followReport = new AxdrOptional<>(new FollowReport(), false);
    private AxdrOptional<TimeTag> timeTag = new AxdrOptional<>(new TimeTag(), false);

    public FjSApdu() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setFollowTimetag(FollowReport followReport, TimeTag timeTag) {
        this.followReport = new AxdrOptional<>(new FollowReport(), false);
        this.timeTag = new AxdrOptional<>(new TimeTag(), false);
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.timeTag = new AxdrOptional<>(new TimeTag(), false);
        codeLength += this.timeTag.decode(input);
        this.followReport = new AxdrOptional<>(new FollowReport(), false);
        codeLength += this.followReport.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.timeTag != null && this.followReport != null) {
                codeLength += this.timeTag.encode(output);
                codeLength += this.followReport.encode(output);
            } else {
                throw new Exception("timetag or followReport is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    @Override
    public String toString() {
        if (this.timeTag != null && this.followReport != null) {
            return "sequence: {" + "timeTag: " + this.timeTag + "followReport: " + this.followReport + "}";
        } else {
            return "timeTag or followReport is null";
        }
    }
}

// Additional classes like FollowReport, TimeTag, AxdrType, ReverseByteArrayInputStream, and ReverseByteArrayOutputStream
// need to be defined in Java as well, with appropriate methods and constructors.



